#pragma once
#include"Date.h"

using namespace bit;

namespace bit 
{
	template<class T>
	class Greater
	{
	public:
		bool operator()(const T& x, const T& a)
		{
			return x < a;
		}
	};

	template<class T>
	class Less
	{
	public:
		bool operator()(const T& x, const T& a)
		{
			return x > a;
		}
	};

	template<>
	class Less<Date*>
	{
	public:
		bool operator()(Date* const & x, const Date* const & a)
		{
			return *x > *a;
		}
	};


	template<class T, class Container = vector<T>, class Comapre = Less<T>>
	class priority_queue
	{
	public:
		priority_queue() = default;

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				_val.push_back(*first);
				first++;
			}
			int child = _val.size() - 1;
			for (int i = (child - 1) / 2; i >= 0; i--)
			{
				adjust_down(i);
			}
		}


		bool empty()
		{
			return _val.size() == 0;
		}

		const T& top()
		{
			return _val.front();
		}

		size_t size()
		{
			return _val.size();
		}

		void adjust_up(size_t child)
		{
			Comapre a1;
			size_t parent = (child - 1) / 2;
			while(child > 0)
			{
				if (a1(_val[child], _val[parent]))
				{
					swap(_val[child], _val[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_val.push_back(x);
			adjust_up(size() - 1);
		}

		void adjust_down(int x)
		{
			Comapre a1;
			size_t parent = x;
			size_t child = 2 * parent + 1;
			while (child < size())
			{
				if (child+1<size()&&(a1(_val[child + 1], _val[child])))
				{
					child++;
				}
				if (a1(_val[child], _val[parent]))
				{
					swap(_val[parent], _val[child]);
					parent = child;
					child = 2 * parent + 1;
				}
				else
				{
					break;
				}
			}
		}

		void pop()
		{
			swap(_val[0], _val[size() - 1]);
			_val.pop_back();
			adjust_down(0);
		}
	private:
		Container _val;
	};
}
